// pages/market/market-order-create.js
var http = require("../../utils/http.js")

import api from "../../utils/api.js";
import market_cart from '../../utils/market/market_cart'
import message from "../../utils/message.js";

Page({

    /**
     * 页面的初始数据
     */
    data: {

        cart_good_list:[],
        total_price:0,
        address:null,
        exp_type:1,
        order_info:null,
        is_select_coupou:false,
        coupou:null,
        coupou_list:[],
        is_first:true,
        message:"",
        address:null

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var me = this;
        setTimeout(function (params) {
            me.setData({
                is_first: false
            })
        } , 500)

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getMarketCartGood();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getMarketCartGood:function(){
        var me = this;
        market_cart.preOrder( {} , function( response ){
            if(response.status==1){
                me.setData({
                    order_info:response.data
                })
            }

        })
    },
    doOrderCreate:function(){
        //创建订单
        var me = this;

        //var post_data = {};
        var address = this.data.address;


        if(this.data.exp_type==1){

            if(address==null){
                message.show_error("请选择收货地址")
                return;
            }
        }

        
        

        var data = {}

        if( this.data.order_info.user_coupou_id!=null ){
            data.user_coupou_id = this.data.order_info.user_coupou_id;
        }

        if(this.data.exp_type==1){
            data.address_id = address.id;
        }

        data.exp_type = this.data.exp_type;
        data.message = this.data.message;
        

        market_cart.createMarketOrder( data  , function( response ){
            console.log(response)
            if(response.status==1){
                message.showPay(function(res){
                    if(res.confirm){
                        console.log("去支付")
                        me.doMarkerOrderPay( response.data.order_no )
                    }else{
                        //console.log("去查看");

                        wx.navigateTo({
                          url: '/pages/market/order/detail?order_no='+response.data.order_no,
                        })
                    }
                })
            }else{
                message.show_error( response.message);
            }

        })

    },
    doMarkerOrderPay:function(order_no){
        var me = this;

        market_cart.doMarkerOrderPay({order_no:order_no} , function(response){

            if(response.status==1){

                me.appletPay(response.data , order_no )

            }else{

            }

        })

    },

    appletPay:function( payInfo , order_no ){
        //调用微信支付信息，勾起订单支付
        var me = this;
        wx.requestPayment({
          "timeStamp": payInfo.timeStamp.toString(),
          "nonceStr": payInfo.nonceStr,
          "package": payInfo.package,
          "signType": payInfo.signType,
          "paySign": payInfo.paySign,
          success:function( res ){
              console.log(res);
              //me.getOrderDetail(order_no);
    
              //me.getOrderList(0)

              //支付完成， 校验支付结果
              message.show_error("订单支付完成，商家确认后安排配送" , function(  ){
                  wx.navigateBack({
                    delta: 1,
                  })
              })

              //console.log(order_no)
          },
          fail:function(res){
              console.log(res)
          }
        })
    
      },
    selectOrderExpType:function( e ){
        var exp_type = e.currentTarget.dataset.type;
        if(exp_type!=1 && exp_type!=2){
            message.error("请选择配送方式");
            return ;
        }

        this.setData({
            exp_type:exp_type

        })

    },
    selectCoupou:function( e ){
        var me = this;
        //选择使用优惠券
        var action = e.currentTarget.dataset.action;
        if(action==1){

            this.setData({
                is_select_coupou:true
            })
            this.getUserCoupouList();
        }

        if(action==-1){

            this.setData({
                is_select_coupou:false,
                coupou:null
            })

            market_cart.preOrder({ } , function( response ){
                if( response.status==1 ){
                    me.setData({
                        order_info: response.data,
                        is_select_coupou:false
                    })
                    
                }else{

                }
            })
        }

        if(action==2){
            var user_coupou_id = e.currentTarget.dataset.id;

            market_cart.preOrder({ user_coupou_id :user_coupou_id} , function( response ){
                if( response.status==1 ){
                    me.setData({
                        order_info: response.data,
                        is_select_coupou:false
                    })
                    
                }else{

                }
            })

        }
    },
    getUserCoupouList:function(){
        var me = this;
        api.getUserCanUseCoupouList( {} , function( response ){
            console.log(response);
            if(response.status==1){
                me.setData({
                    coupou_list: response.data
                })
            }else{
                message.show_error( response.message )
            }

        })
    },
    setUserAddress:function (address) {
        this.setData({
            address: address
        })
    },
    doOrderRemarkInput:function (e) {
        var value = e.detail.value;
        this.setData({
            message: value
        })
        
    },
    doPhoneCall:function (e) {
        var mobile =e.currentTarget.dataset.mobile;
        wx.makePhoneCall({
          phoneNumber: mobile,
        })
    }
})